/*
 * Project: Set Sample
 * Copyright (C) 2014 alf.labs gmail com,
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.alflabs.setsample.ui;

import android.content.Intent;
import android.widget.Button;
import com.alflabs.setsample.R;
import com.alflabs.setsample.RobolectricGradleTestRunner;
import com.alflabs.setsample.stats.GAHelper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.shadows.ShadowActivity;
import org.robolectric.shadows.ShadowIntent;
import org.robolectric.util.ActivityController;

import static org.fest.assertions.api.ANDROID.assertThat;
import static org.junit.Assert.assertEquals;

@RunWith(RobolectricGradleTestRunner.class)
public class IntroActivityTestlectric {

    private ActivityController<IntroActivity> mController;
    private IntroActivity mActivity;

    @Before
    public void setup() {
        // turn GA off when testing
        GAHelper.sEnabled_NotForTesting = false;

        mController = Robolectric.buildActivity(IntroActivity.class);
        mController.create()
                   .start()
                   .resume()
                   .visible();
        mActivity = mController.get();
    }

    @Test
    public void shouldNotBeNull() {
        assertThat(mActivity).isNotNull();

        Button btnSettings = (Button) mActivity.findViewById(R.id.btnSettings);
        assertThat(btnSettings).isNotNull();

        Button btnHelp = (Button) mActivity.findViewById(R.id.btnHelp);
        assertThat(btnHelp).isNotNull();

        Button btnStats = (Button) mActivity.findViewById(R.id.btnStats);
        assertThat(btnStats).isNotNull();

        Button btnPlay = (Button) mActivity.findViewById(R.id.btnPlay);
        assertThat(btnPlay).isNotNull();
    }

    @Test
    public void callSettings() {
        Button btnSettings = (Button) mActivity.findViewById(R.id.btnSettings);
        btnSettings.performClick();

        ShadowActivity shadowActivity = Robolectric.shadowOf(mActivity);
        Intent intent = shadowActivity.getNextStartedActivity();

        ShadowIntent shadowIntent = Robolectric.shadowOf(intent);
        assertEquals(SettingsActivity.class.getName(), shadowIntent.getComponent().getClassName());
    }

    @Test
    public void callHelp() {
        Button btnHelp = (Button) mActivity.findViewById(R.id.btnHelp);
        btnHelp.performClick();

        ShadowActivity shadowActivity = Robolectric.shadowOf(mActivity);
        Intent intent = shadowActivity.getNextStartedActivity();

        ShadowIntent shadowIntent = Robolectric.shadowOf(intent);
        assertEquals(HelpActivity.class.getName(), shadowIntent.getComponent().getClassName());
    }

    @Test
    public void callStats() {
        Button btnStats = (Button) mActivity.findViewById(R.id.btnStats);
        btnStats.performClick();

        ShadowActivity shadowActivity = Robolectric.shadowOf(mActivity);
        Intent intent = shadowActivity.getNextStartedActivity();

        ShadowIntent shadowIntent = Robolectric.shadowOf(intent);
        assertEquals(SetStatsActivity.class.getName(), shadowIntent.getComponent().getClassName());
    }

    @Test
    public void callPlay() {
        Button btnPlay = (Button) mActivity.findViewById(R.id.btnPlay);
        btnPlay.performClick();

        ShadowActivity shadowActivity = Robolectric.shadowOf(mActivity);
        Intent intent = shadowActivity.getNextStartedActivity();

        ShadowIntent shadowIntent = Robolectric.shadowOf(intent);
        assertEquals(SetActivity.class.getName(), shadowIntent.getComponent().getClassName());
    }

}
